package com.wayhome.srpingbootmybatis.boc.controller;

import com.wayhome.srpingbootmybatis.boc.dto.BocRefundOrdReqDTO;
import com.wayhome.srpingbootmybatis.boc.dto.CodePayFixmedinsApiRespDO;
import com.wayhome.srpingbootmybatis.boc.dto.PayFlagEmum;
import com.wayhome.srpingbootmybatis.boc.dto.RetCodeEmum;
import com.wayhome.srpingbootmybatis.boc.service.BocNewChannelImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/boc")
public class BocController {

    @Resource
    private BocNewChannelImpl bocNewChannel;

    @GetMapping(value = "/b2cPay")
    public CodePayFixmedinsApiRespDO b2cPay(String authCode) throws Exception {
        try {
          return bocNewChannel.billpay(authCode);
        }catch (Exception e) {
            log.info("==========>一码付请求中行付款接口发生异常 异常信息：{}", e.getMessage(), e);
            CodePayFixmedinsApiRespDO codePayFixmedinsApiRespDO = new CodePayFixmedinsApiRespDO();
            codePayFixmedinsApiRespDO.setRetCode(RetCodeEmum.RET_CODE_1.getCode());
            codePayFixmedinsApiRespDO.setPayFlag(PayFlagEmum.PAY_FLAG_EMUM_0.getCode());
            codePayFixmedinsApiRespDO.setRetMsg(e.getMessage());
            return codePayFixmedinsApiRespDO;
        }
    }

    @GetMapping(value = "/b2cPayQuery")
    public CodePayFixmedinsApiRespDO b2cPayQueery(String oldPayLs) throws Exception {

        try {
            return bocNewChannel.billpayQuery(oldPayLs);
        }catch (Exception e) {
            log.info("==========>一码付请求中行订单查询接口发生异常 异常信息：{}", e.getMessage(), e);
            CodePayFixmedinsApiRespDO codePayFixmedinsApiRespDO = new CodePayFixmedinsApiRespDO();
            codePayFixmedinsApiRespDO.setRetCode(RetCodeEmum.RET_CODE_1.getCode());
            codePayFixmedinsApiRespDO.setPayFlag(PayFlagEmum.PAY_FLAG_EMUM_0.getCode());
            codePayFixmedinsApiRespDO.setRetMsg(e.getMessage());
            return codePayFixmedinsApiRespDO;
        }
    }

    @PostMapping(value = "/b2cPayRefund")
    public CodePayFixmedinsApiRespDO b2cPayRefund(@RequestBody BocRefundOrdReqDTO bocRefundOrdReqDTO) throws Exception {
        try {
            return bocNewChannel.b2cPayRefund(bocRefundOrdReqDTO);
        }catch (Exception e) {
            log.info("==========>一码付请求中行退款接口发生异常 异常信息：{}", e.getMessage(), e);
            CodePayFixmedinsApiRespDO codePayFixmedinsApiRespDO = new CodePayFixmedinsApiRespDO();
            codePayFixmedinsApiRespDO.setRetCode(RetCodeEmum.RET_CODE_1.getCode());
            codePayFixmedinsApiRespDO.setReturnFlag(PayFlagEmum.PAY_FLAG_EMUM_0.getCode());
            codePayFixmedinsApiRespDO.setRetMsg(e.getMessage());
            return codePayFixmedinsApiRespDO;
        }

    }

    @PostMapping(value = "/b2cPayRefundQuery")
    public CodePayFixmedinsApiRespDO b2cPayRefundQuery(@RequestBody BocRefundOrdReqDTO bocRefundOrdReqDTO) throws Exception {

        try {
            return bocNewChannel.b2cPayRefundQuery(bocRefundOrdReqDTO);
        }catch (Exception e) {
            log.info("==========>一码付请求中行退款查询接口发生异常 异常信息：{}", e.getMessage(), e);
            CodePayFixmedinsApiRespDO codePayFixmedinsApiRespDO = new CodePayFixmedinsApiRespDO();
            codePayFixmedinsApiRespDO.setRetCode(RetCodeEmum.RET_CODE_1.getCode());
            codePayFixmedinsApiRespDO.setReturnFlag(PayFlagEmum.PAY_FLAG_EMUM_0.getCode());
            codePayFixmedinsApiRespDO.setRetMsg(e.getMessage());
            return codePayFixmedinsApiRespDO;
        }
    }
}
